import DrawElements from "@/utils/DrawElements"

export default class DrawElementsFactory {
	constructor(drawPageParams) {
		this.drawPageParams = drawPageParams
		this.clickEventList = []
		this.boxWidth = 25
		this.boxHeight = 25
		this.lineWidth = 40
		this.leftX = 15
		this.cyJJ = 8
		this.rowJJ = 20
		this.initY = 0
		this.maxRowCount = 9
		this.rowMaxWords = 10
		this.isNewRow = true
		this.drawWordsCount = 0
		this.drawCYCount = 0
		this.drawRowIndex = 1
		this.titleInitY = 90
		this.noTitleInitY = 30
		this.drawElementsPageList = []
	}
	createDrawElements() {//355,502
		this.boxWidth = (this.boxWidth/355) *this.drawPageParams.canvasWidth
		this.boxHeight = (this.boxHeight/502) *this.drawPageParams.canvasHeight
	
		this.initY = (this.titleInitY/502) *this.drawPageParams.canvasHeight
		this.titleInitY = (this.titleInitY/502) *this.drawPageParams.canvasHeight
		this.rowJJ = (this.rowJJ/502) *this.drawPageParams.canvasHeight
		this.cyJJ = (this.cyJJ/355) *this.drawPageParams.canvasWidth
		this.leftX = (this.leftX/355) *this.drawPageParams.canvasWidth
        this.lineWidth = (this.lineWidth/355) * this.drawPageParams.canvasWidth
		let lineDashWidth =  (2/355) *this.drawPageParams.canvasWidth
		let lineDashPadWidth =  (1/355) *this.drawPageParams.canvasWidth
		let pageInitY = (30/502) *this.drawPageParams.canvasHeight
		let fontWidth = 0
		let fontHeight = 0
		let clickCount = 0
		let ex = 0
		let ey = 0
		let drawElementsList = []
		let drawElements = new DrawElements()
		let pinyinFontSize  = this.drawPageParams.canvasWidth>700?18:10
		let hanziFontSize  = this.drawPageParams.canvasWidth>700?30:17
		let x = 0
		let y = 0
		let moveX = 0
		let wordWidth =16
		drawElements = new DrawElements()
		drawElements.text = this.drawPageParams.title
		drawElements.type = 5
		drawElements.color = "black"
		drawElements.fontSize = "18"
		drawElements.y = 40
		drawElements.align = 'center'
		drawElementsList.push(drawElements);
		
		//绘制年级
		drawElements = new DrawElements()
		drawElements.text = "年级"
		drawElements.type = 5
		drawElements.color = "black"
		drawElements.fontSize = "16"
		drawElements.x = 20
		moveX = drawElements.x
		drawElements.y = 65
		drawElementsList.push(drawElements);
		
		//绘制年级下划线
		drawElements = new DrawElements()
		drawElements.type = 6
		drawElements.lw = 0.5
		drawElements.color = "black"
		drawElements.lineX = moveX + wordWidth * 2.3
		drawElements.lineY = 65
		drawElements.lineToX = drawElements.lineX+ this.lineWidth
		moveX = drawElements.lineToX
		drawElements.lineToY = 65
		drawElementsList.push(drawElements);
		
		//绘制班级
		drawElements = new DrawElements()
		drawElements.text = "班级"
		drawElements.type = 5
		drawElements.color = "black"
		drawElements.fontSize = "16"
		drawElements.x = moveX + 5
		drawElements.y = 65
		drawElementsList.push(drawElements)
		
		//绘制年级下划线
		drawElements = new DrawElements()
		drawElements.type = 6
		drawElements.lw = 0.5
		drawElements.color = "black"
		drawElements.lineX = moveX + wordWidth * 2.5
		drawElements.lineY = 65
		drawElements.lineToX = drawElements.lineX+ this.lineWidth
		moveX = drawElements.lineToX
		drawElements.lineToY = 65
		drawElementsList.push(drawElements);
		
		
		//绘制姓名
		drawElements = new DrawElements()
		drawElements.text = "姓名"
		drawElements.type = 5
		drawElements.color = "black"
		drawElements.fontSize = "16"
		drawElements.x = moveX + 5
		drawElements.y = 65
		drawElementsList.push(drawElements)
		
		//绘制姓名下划线
		drawElements = new DrawElements()
		drawElements.type = 6
		drawElements.lw = 0.5
		drawElements.color = "black"
		drawElements.lineX = moveX + wordWidth * 2.5
		drawElements.lineY = 65
		drawElements.lineToX = drawElements.lineX+ this.lineWidth
		moveX = drawElements.lineToX
		drawElements.lineToY = 65
		drawElementsList.push(drawElements);
		
		//绘制成绩
		drawElements = new DrawElements()
		drawElements.text = "成绩"
		drawElements.type = 5
		drawElements.color = "black"
		drawElements.fontSize = "16"
		drawElements.x = moveX + 5
		drawElements.y = 65
		drawElementsList.push(drawElements)
		
		//绘制年级下划线
		drawElements = new DrawElements()
		drawElements.type = 6
		drawElements.lw = 0.5
		drawElements.color = "black"
		drawElements.lineX = moveX + wordWidth * 2.5
		drawElements.lineY = 65
		drawElements.lineToX = drawElements.lineX+ this.lineWidth
		moveX = drawElements.lineToX
		drawElements.lineToY = 65
		drawElementsList.push(drawElements);
		
		for (let i = 0; i < this.drawPageParams.getDrawDataSource().pinyinList.length; i++) {
			for (let j = 0; j < this.drawPageParams.getDrawDataSource().wordList[i].length; j++) {
				if (j == 0 && this.drawWordsCount + this.drawPageParams.getDrawDataSource().wordList[i].length >this.rowMaxWords) { //换行
					this.drawWordsCount = 0;
					this.drawCYCount = 0;
					this.drawRowIndex++;
					this.initY = this.initY + this.boxHeight + this.rowJJ;
					if (this.drawRowIndex > this.maxRowCount) {
						this.drawRowIndex = 0;
						this.initY = (this.noTitleInitY/502) *this.drawPageParams.canvasHeight
						this.drawElementsPageList.push(drawElementsList)
						drawElementsList = []
					}
				}
				//方格
				if(this.drawPageParams.boxType == 1){
					drawElements = new DrawElements()
					drawElements.type = 1
					drawElements.setW(this.boxWidth);
					drawElements.setH(this.boxHeight);
					drawElements.setColor(this.drawPageParams.getBoxColor());
					drawElements.setLW(1);
					drawElements.setX(this.leftX + this.drawWordsCount * this.boxWidth + this.drawCYCount * this.cyJJ);
					drawElements.setY(this.initY);
					drawElementsList.push(drawElements);
				}
				//田字格
				if(this.drawPageParams.boxType == 2){
					x = this.leftX + this.drawWordsCount * this.boxWidth + this.drawCYCount * this.cyJJ
					y = this.initY
					drawElements = new DrawElements()
					drawElements.type = 1
					drawElements.setW(this.boxWidth);
					drawElements.setH(this.boxHeight);
					drawElements.setColor(this.drawPageParams.getBoxColor())
					drawElements.setLW(1);
					drawElements.setX(x);
					drawElements.setY(y);
					drawElementsList.push(drawElements);
					//画田字横线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x
					drawElements.lineY = y+ this.boxHeight/2
					drawElements.lineToX = x + this.boxWidth
					drawElements.lineToY = y + this.boxHeight/2
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
				    drawElementsList.push(drawElements);
					
					//画田字竖线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x + this.boxWidth/2
					drawElements.lineY = y
					drawElements.lineToX = x + this.boxWidth/2
					drawElements.lineToY = y + this.boxHeight
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
					drawElementsList.push(drawElements);
				}
				
				//米字格
				if(this.drawPageParams.boxType == 3){
					x = this.leftX + this.drawWordsCount * this.boxWidth + this.drawCYCount * this.cyJJ
					y = this.initY
					drawElements = new DrawElements()
					drawElements.type = 1
					drawElements.setW(this.boxWidth);
					drawElements.setH(this.boxHeight);
					drawElements.setColor(this.drawPageParams.getBoxColor())
					drawElements.setLW(1);
					drawElements.setX(x);
					drawElements.setY(y);
					drawElementsList.push(drawElements);
					//画田字横线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x
					drawElements.lineY = y+ this.boxHeight/2
					drawElements.lineToX = x + this.boxWidth
					drawElements.lineToY = y + this.boxHeight/2
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
				    drawElementsList.push(drawElements);
					
					//画田字竖线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x + this.boxWidth/2
					drawElements.lineY = y
					drawElements.lineToX = x + this.boxWidth/2
					drawElements.lineToY = y + this.boxHeight
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
					drawElementsList.push(drawElements);
					
					//画米字右斜线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x
					drawElements.lineY = y
					drawElements.lineToX = x + this.boxWidth
					drawElements.lineToY = y + this.boxHeight
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
					drawElementsList.push(drawElements);
					
					//画米字左斜线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x + this.boxWidth
					drawElements.lineY = y
					drawElements.lineToX = x
					drawElements.lineToY = y + this.boxHeight
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
					drawElementsList.push(drawElements);
					
					
				}
				
				//拼音
				drawElements = new DrawElements()
				drawElements.type = 2
				drawElements.color = "black";
				drawElements.fontSize = pinyinFontSize
				drawElements.setW(this.boxWidth);
				drawElements.text = this.drawPageParams.getDrawDataSource().pinyinList[i][j]
				fontWidth = drawElements.text.length * ((5/355) * this.drawPageParams.canvasWidth)
				fontHeight = drawElements.text.length * ((5/502) * this.drawPageParams.canvasHeight)
				drawElements.setX(this.leftX + this.drawWordsCount * this.boxWidth + this.drawCYCount * this.cyJJ + (this.boxWidth - fontWidth) /2);
				drawElements.setY(this.initY-5/502 * this.drawPageParams.canvasHeight);
				
				drawElementsList.push(drawElements);
				//汉字
				drawElements = new DrawElements()
				drawElements.type = 3
				drawElements.fontSize = hanziFontSize
				drawElements.fontType = '2'
				drawElements.text = this.drawPageParams.getDrawDataSource().wordList[i][j]        
				drawElements.x = this.leftX + this.drawWordsCount * this.boxWidth + this.drawCYCount * this.cyJJ + (this.boxWidth - wordWidth)/2
				drawElements.y = this.initY+this.boxHeight*12/16
				ex = drawElements.x + 10 
				ey = drawElements.y + 10 + this.drawElementsPageList.length *  this.drawPageParams.canvasHeight + this.drawElementsPageList.length * 10
				drawElements.ex = ex
				drawElements.ey = ey
				
				clickCount = this.getclickCount(ex,ey)
				if(clickCount>0 && clickCount%2!=0)
				{
					drawElements.color = "#CC3300";
				}
				else
				{
					drawElements.color = "#C5C1AA";
					
				}
				drawElementsList.push(drawElements);
				
				
			
				this.drawWordsCount++;
			}
			this.drawCYCount++;
		}
		
		
		//答案页面
		this.drawElementsPageList.push(drawElementsList)
		drawElementsList = []
		this.drawWordsCount = 0;
		this.drawCYCount = 0;
		this.drawRowIndex = 1;
		this.initY = this.titleInitY
		
		drawElements = new DrawElements()
		drawElements.text = this.drawPageParams.title
		drawElements.type = 5
		drawElements.color = "black"
		drawElements.fontSize = "18"
		drawElements.y = 40
		drawElements.align = 'center'
		drawElementsList.push(drawElements)
		
		drawElements = new DrawElements()
		drawElements.text = "（答案）"
		drawElements.type = 5
		drawElements.color = "black"
		drawElements.fontSize = "18"
		drawElements.y = 60
		drawElements.align = 'center'
		drawElementsList.push(drawElements);
		
		for (let i = 0; i < this.drawPageParams.getDrawDataSource().pinyinList.length; i++) {
			for (let j = 0; j < this.drawPageParams.getDrawDataSource().wordList[i].length; j++) {
				if (j == 0 && this.drawWordsCount + this.drawPageParams.getDrawDataSource().wordList[i].length >this.rowMaxWords) { //换行
					this.drawWordsCount = 0;
					this.drawCYCount = 0;
					this.drawRowIndex++;
					this.initY = this.initY + this.boxHeight + this.rowJJ;
					if (this.drawRowIndex > this.maxRowCount) {
						this.drawRowIndex = 0;
						this.initY = (this.noTitleInitY/502) *this.drawPageParams.canvasHeight
						this.drawElementsPageList.push(drawElementsList)
						drawElementsList = []
					}
				}
			
				//方格
				if(this.drawPageParams.boxType == 1){
					drawElements = new DrawElements()
					drawElements.type = 1
					drawElements.setW(this.boxWidth);
					drawElements.setH(this.boxHeight);
					drawElements.setColor(this.drawPageParams.getBoxColor());
					drawElements.setLW(1);
					drawElements.setX(this.leftX + this.drawWordsCount * this.boxWidth + this.drawCYCount * this.cyJJ);
					drawElements.setY(this.initY);
					drawElementsList.push(drawElements);
				}
				//田字格
				if(this.drawPageParams.boxType == 2){
					x = this.leftX + this.drawWordsCount * this.boxWidth + this.drawCYCount * this.cyJJ
					y = this.initY
					drawElements = new DrawElements()
					drawElements.type = 1
					drawElements.setW(this.boxWidth);
					drawElements.setH(this.boxHeight);
					drawElements.setColor(this.drawPageParams.getBoxColor())
					drawElements.setLW(1);
					drawElements.setX(x);
					drawElements.setY(y);
					drawElementsList.push(drawElements);
					//画田字横线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x
					drawElements.lineY = y+ this.boxHeight/2
					drawElements.lineToX = x + this.boxWidth
					drawElements.lineToY = y + this.boxHeight/2
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
				    drawElementsList.push(drawElements);
					
					//画田字竖线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x + this.boxWidth/2
					drawElements.lineY = y
					drawElements.lineToX = x + this.boxWidth/2
					drawElements.lineToY = y + this.boxHeight
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
					drawElementsList.push(drawElements);
				}
				
				//米字格
				if(this.drawPageParams.boxType == 3){
					x = this.leftX + this.drawWordsCount * this.boxWidth + this.drawCYCount * this.cyJJ
					y = this.initY
					drawElements = new DrawElements()
					drawElements.type = 1
					drawElements.setW(this.boxWidth);
					drawElements.setH(this.boxHeight);
					drawElements.setColor(this.drawPageParams.getBoxColor())
					drawElements.setLW(1);
					drawElements.setX(x);
					drawElements.setY(y);
					drawElementsList.push(drawElements);
					//画田字横线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x
					drawElements.lineY = y+ this.boxHeight/2
					drawElements.lineToX = x + this.boxWidth
					drawElements.lineToY = y + this.boxHeight/2
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
				    drawElementsList.push(drawElements);
					
					//画田字竖线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x + this.boxWidth/2
					drawElements.lineY = y
					drawElements.lineToX = x + this.boxWidth/2
					drawElements.lineToY = y + this.boxHeight
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
					drawElementsList.push(drawElements);
					
					//画米字右斜线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x
					drawElements.lineY = y
					drawElements.lineToX = x + this.boxWidth
					drawElements.lineToY = y + this.boxHeight
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
					drawElementsList.push(drawElements);
					
					//画米字左斜线
					drawElements = new DrawElements()
					drawElements.type = 4
					drawElements.color = this.drawPageParams.getBoxColor()
					drawElements.lw = 1
					drawElements.lineX = x + this.boxWidth
					drawElements.lineY = y
					drawElements.lineToX = x
					drawElements.lineToY = y + this.boxHeight
					drawElements.lineDashWidth = lineDashWidth
					drawElements.lineDashPadWidth = lineDashPadWidth
					drawElementsList.push(drawElements);
					
					
				}
				
				//拼音
				drawElements = new DrawElements()
				drawElements.type = 2
				drawElements.color = "black";
				drawElements.fontSize = pinyinFontSize
				drawElements.setW(this.boxWidth);
				drawElements.text = this.drawPageParams.getDrawDataSource().pinyinList[i][j]
				fontWidth = drawElements.text.length * ((5/355) * this.drawPageParams.canvasWidth)
				fontHeight = drawElements.text.length * ((5/502) * this.drawPageParams.canvasHeight)
				drawElements.setX(this.leftX + this.drawWordsCount * this.boxWidth + this.drawCYCount * this.cyJJ + (this.boxWidth - fontWidth) /2);
				drawElements.setY(this.initY-5/502 * this.drawPageParams.canvasHeight);
				drawElementsList.push(drawElements);
				
				//汉字
				drawElements = new DrawElements()
				drawElements.type = 3
				drawElements.setColor("#aa2116");
				drawElements.fontType = "1"
				drawElements.fontSize = hanziFontSize
				drawElements.setW(this.boxWidth);
				drawElements.text = this.drawPageParams.getDrawDataSource().wordList[i][j]
	        
				drawElements.setX(this.leftX + this.drawWordsCount * this.boxWidth + this.drawCYCount * this.cyJJ +(this.boxWidth-wordWidth)/2)
				drawElements.setY(this.initY+this.boxHeight*12/16);
				drawElementsList.push(drawElements);
				this.drawWordsCount++;
			}
			this.drawCYCount++;
		}
		
		
		this.drawElementsPageList.push(drawElementsList);
		return this
	}
	
	geteDrawElements()
	{
		return this.drawElementsPageList
	}
	
	getclickCount(x,y)
	{
		let count = 0
		for (var i = 0; i < this.clickEventList.length; i++) {
			if(this.clickEventList[i].x >= x && this.clickEventList[i].x <= x+this.boxWidth && this.clickEventList[i].y >= y && this.clickEventList[i].y <= y+this.boxHeight)
			{
				count++
				
			}
		}
		return count
	}

	
}